package com.paradogs.gs.modules;

import com.paradogs.common._cfg.Cfg;
import com.paradogs.common.utils.SpringUtils;
import com.paradogs.gs.misc._NetMgr;
import com.paradogs.gs.modules.world.base.WorldUnitAbility;
import com.paradogs.gs.modules.world.base.WorldUnitLiftCycle;
import com.paradogs.gs.modules.world.base.unit.WorldDrop;
import com.paradogs.gs.modules.world.entity.BigWorld;
import com.paradogs.gs.modules.world.WorldService;
import com.paradogs.gs.modules.world.entity.WorldNode;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: yumi
 * @date: 2025/4/13  13:37
 * @Description: TODO
 */
@Data
public abstract class WorldObject implements WorldUnitLiftCycle, WorldUnitAbility {
    private GameObject gameObject;
    // 方向 1上2右3下4左
    private static final int[][] DIRECTION = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    private WorldService worldService;
    private Long id;
    private int worldId;
    private int x;
    private int y;
    private UnitType unitType;
    // fight
    private int hp;
    private int maxHp;
    private int mp;
    private int maxMp;

    public enum UnitType {
        PLAYER(1),
        DROP(2),
        NPC(3),
        MONSTER(4);
        private static final Map<Integer, UnitType> map = new HashMap<>();
        private final int unitType;
        static { for (UnitType type : values()) { map.put(type.unitType, type); } }
        UnitType(int unitType) { this.unitType = unitType; }
        public static UnitType of(int unitType) { return map.get(unitType); }
    }

    public WorldObject() {
        this.worldService = SpringUtils.getBean(WorldService.class);
    }

    public void load(Long id, int world, int x, int y, int hp, int maxHp, int mp, int maxMp, UnitType unitType) {
        this.id = id;
        this.worldId = world;
        this.x = x;
        this.y = y;
        this.hp = hp;
        this.maxHp = maxHp;
        this.mp = mp;
        this.maxMp = maxMp;
        this.unitType = unitType;
    }

    public abstract WorldSyncData toSyncData();

    public void born() {
        teleport(worldId, x, y);
    }

    public void teleport(int world, int x, int y) {
        leave(world, x, y);
        enter(world, x, y);
    }

    public void leave(int worldId, int x, int y) {
        BigWorld world = worldService.getBigWorld(worldId);
        world.leave(this, x, y);
    }

    public void enter(int worldId, int x, int y) {
        BigWorld world = worldService.getBigWorld(worldId);
        world.enter(this, x, y);
    }

    public void move(Integer dir) {
        int[] xy = DIRECTION[dir - 1];
        leave(worldId, x, y);
        enter(worldId, x + xy[0], y + xy[1]);
    }

    public BigWorld curWorld() {
        return worldService.getBigWorld(worldId);
    }
    public WorldNode curNode() {
        return curWorld().getNodeMap().get(WorldNode.key(x, y));
    }


    // 被伤害 TODO 放 FightCycleLift 里
    @Override
    public void onBeHurt(WorldObject from) {
        this.hp = this.hp - 500;
        // TODO 先简单同步状态，后面也改成 update() + 自动同步
        WorldSyncData syncData = new WorldSyncData();
        syncData.setId(getId());
        syncData.setCurHp(this.hp);
        curNode().updateObject(syncData);

        // 死亡
        if (this.hp <= 0) {
            onDead(from);
        }
    }

    public boolean isPlayer() {
        return unitType == UnitType.PLAYER;
    }
    public Player getPlayer() {
        return (Player) gameObject;
    }

}
